from flask import Flask, request, jsonify, jsonify
from flask.json import loads as jsonloads
import requests
import random
import string

app = Flask(__name__)

def generate_password(length=4):
    # 定义字符集，包括字母和数字
    characters = string.ascii_letters + string.digits
    # 从字符集中随机选择指定长度的字符
    password = ''.join(random.choice(characters) for i in range(length))
    return password

def verify_baidu_share(pwd, surl):
    url = 'https://pan.baidu.com/share/verify'
    params = {
        'surl': surl
    }
    headers = {
        'Origin': 'https://pan.baidu.com',
        'Referer': 'https://pan.baidu.com/',
    }
    data = {
        'pwd': pwd
    }

    response = requests.post(url, params=params, headers=headers, data=data)
    return response.text

@app.route('/verify', methods=['GET'])
def verify():
    surl = request.args.get('surl')
    pwd = request.args.get('pwd')
    
    if not surl or not pwd:
        return jsonify({"error": "Missing parameters"}), 400
    
    result = verify_baidu_share(pwd, surl)
    # return 
    print(jsonloads(result)['errno'])
    if jsonloads(result)['errno'] == 0:
        return jsonify({"success": True, "pwd": pwd}), 200
    else:
        return jsonify({"success": False}), 200

if __name__ == '__main__':
    app.run(host='localhost', port=8888)
